package store.lunangangster.engine.impl.util;

import java.io.ByteArrayOutputStream;
import java.io.Closeable;
import java.io.IOException;
import java.io.InputStream;

public class IOUtil {

  public static byte[] getBytes(InputStream is) {
    if (is == null) {
      return new byte[0];
    }
    try (ByteArrayOutputStream bos = new ByteArrayOutputStream(2048)) {
      int read = 0;
      byte[] buf = new byte[2048];
      while ((read = is.read(buf, 0, 2048)) != -1) {
        bos.write(buf, 0, read);
      }
      return bos.toByteArray();
    } catch (IOException e) {
      throw new RuntimeException(e);
    } finally {
      try {
        is.close();
      } catch (Exception ignore) {

      }
    }
  }

  public static void close(Closeable closeable) {
    if (closeable != null) {
      try {
        closeable.close();
      } catch (IOException e) {
        throw new RuntimeException(e);
      }
    }
  }
}
